package org.leetcode.middle.leetcode2;

public class Test
{

    public static void main(String[] args) {

        ListNode first = new ListNode();
        ListNode two = new ListNode();
        ListNode three = new ListNode();
//        ListNode four =new ListNode();
//        ListNode five =new ListNode();
//        ListNode six =new ListNode();
//        ListNode seven =new ListNode();

        first.val=2;
        first.next=two;

        two.val=4;
        two.next=three;

        three.val=9;
        three.next=null;
//
//        four.val=9;
//        four.next=five;
//
//        five.val=9;
//        five.next=six;
//
//        six.val=9;
//        six.next=seven;
//
//        seven.val=9;
//        seven.next=null;

        ListNode first1 = new ListNode();
        ListNode two1 = new ListNode();
        ListNode three1= new ListNode();
        ListNode four1= new ListNode();


        first1.val=5;
        first1.next=two1;

        two1.val=6;
        two1.next=three1;

        three1.val=4;
        three1.next=four1;

        four1.val=9;
        four1.next=null;

        System.out.println("--------------------------");


        String number1 = printNode(first);
        int number3= Integer.parseInt(number1);
        System.out.println(number1);

        String number2 = printNode(first1);
        int number4= Integer.parseInt(number2);
        System.out.println(number2);


        int result =number3+number4;

        System.out.println(result);

        String stringResult = String.valueOf(result);
        String[] resultArray = stringResult.split("");
        int size=stringResult.length();


        int []array=new int[size];

        for (int i=0;i<size;i++)
        {
            array[i]=Integer.parseInt(resultArray[i]);
        }

        ListNode node = getNode(array, 0);
        String s = printNode(node);
        System.out.println(s);

    //    int [] arrayTest={1,2,3};

//        ListNode node = getNode(arrayTest, 0);
//        String s = printNode(node);
//        System.out.println(s);

    }

    public static String printNode(ListNode list)
    {
        String num =String.valueOf(list.val);
        while (list.next!=null)
            {
                String num2 = printNode(list.next);
                num=num+num2;
                break;
            }

        String reverse = new StringBuilder(num).reverse().toString();
        return reverse;

    }

    public static ListNode getNode(int [] array,int index)
    {
        ListNode node = new ListNode();
        while (true)
        {

            node.val=array[array.length-1-index];
            index++;
            if (index!= array.length)
            {
                node.next=getNode(array,index);
            }
            break;
        }

        return node;
    }

}
